/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.d.ui.view.clock;

import com.d.lib.common.widget.TitleLayout;
import com.d.lib.ui.view.clock.ClockSetView;
import com.d.ui.view.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.utils.Color;

public class ClockSetAbilitySlice extends AbilitySlice implements Component.ClickedListener {

  private Text tv_hour, tv_minute;
  private ClockSetView csv_clockset;
  private int mHour, mMinute;

  @Override
  protected void onStart(Intent intent) {
    super.onStart(intent);
    super.setUIContent(ResourceTable.Layout_ability_clockset);

    bindView();
    initConfig();
  }

  private void bindView() {
    TitleLayout titleLayout = (TitleLayout) findComponentById(ResourceTable.Id_tl_title);
    titleLayout.setBackClickListener(this::onBackClick);

    tv_hour = (Text) findComponentById(ResourceTable.Id_tv_hour);
    tv_minute = (Text) findComponentById(ResourceTable.Id_tv_minute);
    csv_clockset = (ClockSetView) findComponentById(ResourceTable.Id_csv_clockset);

    tv_hour.setClickedListener(this::onClick);
    tv_minute.setClickedListener(this::onClick);
  }

  private void initConfig() {
    csv_clockset.setMode(ClockSetView.MODE_HOUR, mHour);
    tv_hour.setText(getFormatTime(mHour));
    tv_minute.setText(getFormatTime(mMinute));
    csv_clockset.setOnSelectListener(new ClockSetView.OnSelectListener() {
      @Override
      public void onSelect(int mode, int value) {
        if (mode == ClockSetView.MODE_HOUR) {
          mHour = value;
          tv_hour.setText(getFormatTime(value));
        } else if (mode == ClockSetView.MODE_MINUTE) {
          mMinute = value;
          tv_minute.setText(getFormatTime(value));
        }
      }
    });
  }

  private String getFormatTime(int value) {
    return value >= 10 ? String.valueOf(value) : "0" + value;
  }

  @Override
  public void onClick(Component v) {
    int resId = v.getId();
    if (ResourceTable.Id_tv_hour == resId) {
      tv_hour.setTextColor(new Color(0xFFFFFFFF));
      tv_minute.setTextColor(new Color(0xFFDFB5B8));
      csv_clockset.setMode(ClockSetView.MODE_HOUR, mHour);
      tv_hour.setText(getFormatTime(mHour));
      tv_minute.setText(getFormatTime(mMinute));

    } else if (ResourceTable.Id_tv_minute == resId) {
      tv_hour.setTextColor(new Color(0xFFDFB5B8));
      tv_minute.setTextColor(new Color(0xFFFFFFFF));
      csv_clockset.setMode(ClockSetView.MODE_MINUTE, mMinute);
      tv_hour.setText(getFormatTime(mHour));
      tv_minute.setText(getFormatTime(mMinute));

    }
  }

  public void onBackClick(Component v) {
    terminate();
  }

}
